<?php

    /**
     * This file is part of the Intelligencer package.
     * For the full copyright and license, please view the LICENSE
     * file that was distributed with this source code.
     * 
     * @author      drapeko <roman.drapeko@gmail.com>
     * @copyright   Roman Drapeko <roman.drapeko@gmail.com>, 2009 - present
     */

    /**
     * This template implements very popular zend autoloading approach.
     * 
     * Example: class called AAA_BBB_CC is located in AAA/BBB/CCC.php
     * 
     * <code>
     *    $template = new itZendStyleTemplate();
     *    $template->setPrefix('C:/My Documents/Project/Lib/')->setPostfix('.php')->load('ClassA');
     * </code>
     * 
     * @author drapeko
     */
    class itZendStyleTemplate extends itTemplate {
        
        protected $prefix = '';
        protected $postfix = '.php';
        protected $namespaces = array(null);
        protected $acceptUnregisteredNamespaces = true;
        protected $isNamespaceAsDir = true;
        
        public function __construct($prefix = '', $postfix = '') {
            if (!empty($prefix)) {
                $this->setPrefix($prefix);
            }
            
            if (!empty($postfix)) {
                $this->setPostfix($postfix);
            }
        }
        
        /**
         * Sets the prefix to the path (path base)
         * @param string $prefix
         * @return itTemplate itself
         */
        public function setPrefix($prefix = '') {
            $this->prefix = $prefix;
            return $this;
        }
        
        /**
         * Returns the prefix
         * @return string
         */
        public function getPrefix() {
            return $this->prefix;
        }
        
        /**
         * Returns the postfix of the file (usually file extension)
         * @param string $postfix
         * @return itTemplate itself
         */
        public function setPostfix($postfix = '') {
            $this->postfix = $postfix;
            return $this;
        }
        
        /**
         * Returns the postfix of the file
         * @return string
         */
        public function getPostfix() {
            return $this->postfix;
        }
        
        /**
         * Sets whether to take namespace in account of creating the path. 
         * If true Namespace\Dir_Class class should be located in Namespace/Dir/Class.php. If false in Dir/Class.
         * 
         * @param boolean $value
         * @return itTemplate itself
         */
        public function setNamespaceAsDir($value = true) {
            $this->isNamespaceAsDir = $value;
            return $this;
        }
        
        /**
         * Returns whether namespace should be treated as dir.
         * @return boolean
         */
        public function isNamespaceAsDir() {
            return $this->isNamespaceAsDir;
        }
        
        /**
         * Sets whether to accept unregistered namespaces. By default Template accepts unregistered namespaces.
         * 
         * @param boolean $accept true
         * @return itTemplate itself
         * @see setRegisteredNamespaces
         */
        public function acceptUnregisteredNamespaces($accept = true) {
            $this->acceptUnregisteredNamespaces = $accept;
            return $this;
        }

        /**
         * Returns whether accepts unregistered namespaces.
         * @return boolean
         */
        public function isAcceptUnregisteredNamespaces() {
            return $this->acceptUnregisteredNamespaces;
        }
        
        /**
         * Registers namespaces. It's very convenient when you want to create template that handles classes only from particular namespaces. 
         * If you want to search for classes that are not in namespaces use 'null'. By default 'null' namespace is registered.
         * Please pay attention that Template accepts unregistered namespaces by default. It means that getPath won't look
         * whether the namespace registered or not registered if you do not tell it explicitly.
         * 
         * For example,
         * I would like my template to handle classes which are in YYY namespaces and which are in none namespaces.
         * <code>
         *  $template = new itZendStyleTemplate();
         *  $template->acceptUnregisteredNamespaces(false)->setRegisteredNamespaces(array(null, 'YYY'));
         * </code>
         * @param mixed $namespaces array or string
         * @return itTemplate itself
         */
        public function setRegisteredNamespaces($namespaces) {
            $this->namespaces = (array)$namespaces;
            return $this;
        }
        
        /**
         * Returns registered namespaces
         * @return array
         */
        public function getRegisteredNamespaces() {
            return $this->namespaces;
        }
        
        /**
         * Returns the exact location of the file by the class name
         * @param string $className
         * @return string
         */
        public function getPath($className) {
            list($namespace, $class) = itUtil::getClassAndNS($className);
            
            $c = $class;
            if ($this->isNamespaceAsDir && $namespace != null) {
                $c = $namespace.DIRECTORY_SEPARATOR.$class;
            }
            $path = $this->prefix.str_replace('_', DIRECTORY_SEPARATOR, $c).$this->postfix;
            if ($this->acceptUnregisteredNamespaces) {      
                return array($path);
            } else {
                if (itUtil::inArrayInsensitive($namespace, $this->namespaces)) {
                    return array($path);
                }
            }
            
            return array();
        }
    }

?>